﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitBase.WinForm;
using YJKPatternSign.StairsRoomSection.DataDefine;
using YJKPatternSign.StairsRoomSection.Kit;
using YJKPatternSign.StairsRoomSection.Kit.Xdata;

namespace YJKPatternSign.StairsRoomSection.Manager
{
	public class SectionStairsRoomManager
	{
        public SectionStairsRoomManager(UIApplication application, WindowHandle hWndRevit, ElementId viewId, int iflg, ExternalCommandData commandData = null)
		{
			this.m_commandData = commandData;
			this.m_uiApp = application;
			SectionStairsRoomManager.g_application = this.m_uiApp.Application;
			this.m_doc = application.ActiveUIDocument.Document;
			SectionStairsRoomManager._hWndRevit = hWndRevit;
			this.m_ViewId = viewId;
			this.m_iflg = iflg;
		}

		private Autodesk.Revit.DB.View GetViewByElementId(Autodesk.Revit.DB.Document doc, ElementId ElemId)
		{
			string name = doc.GetElement(ElemId).Name;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(ViewSection));
			foreach (Element element in filteredElementCollector.WherePasses(elementClassFilter).ToElements())
			{
				ViewSection viewSection = (ViewSection)element;
				if (viewSection.Name.Equals(name))
				{
					return viewSection;
				}
			}
			return null;
		}

		public bool Run()
		{
			SectionViewData sectionViewData = new SectionViewData(this.GetViewByElementId(this.m_doc, this.m_ViewId));
			sectionViewData.InitViewData();
			this.refreshView(sectionViewData);
			prepareElementsCls prepareElementsCls = new prepareElementsCls(sectionViewData, this.m_iflg);
			prepareElementsCls.InitAllElement();
			this.m_uiApp.ActiveUIDocument.ActiveView = sectionViewData.SectionView;
			new FillElementCls(prepareElementsCls, sectionViewData).FilledElement();
			if (this.m_iflg == 1)
			{
				double num = (double)(this.m_doc.ActiveView.Scale / 50);
				SignCommon.m_dBaseLineInterval *= num;
				SignCommon.m_dFirstBaseLineInterval *= num;
				new SignElementCls(this.m_commandData, prepareElementsCls, sectionViewData).sign();
				SignCommon.m_dBaseLineInterval /= num;
				SignCommon.m_dFirstBaseLineInterval /= num;
			}
			return true;
		}

		public bool refreshView(SectionViewData viewdata)
		{
			List<ElementId> list = new List<ElementId>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
			ElementClassFilter item = new ElementClassFilter(typeof(Dimension));
			ElementClassFilter item2 = new ElementClassFilter(typeof(FilledRegion));
			ElementCategoryFilter item3 = new ElementCategoryFilter(BuiltInCategory.OST_GenericAnnotation);
			ElementClassFilter item4 = new ElementClassFilter(typeof(CurveElement));
			LogicalOrFilter logicalOrFilter = new LogicalOrFilter(new List<ElementFilter>
			{
				item,
				item3,
				item2,
				item4
			});
			filteredElementCollector.WherePasses(logicalOrFilter).WhereElementIsNotElementType();
			foreach (Element element in filteredElementCollector)
			{
				ElementId invalidElementId = ElementId.InvalidElementId;
				if (SignXdataForReference.ReadSignCommont(element, ref invalidElementId) && invalidElementId == viewdata.SectionView.Id)
				{
					list.Add(element.Id);
				}
			}
			if (list.Count > 0)
			{
				Transaction transaction = new Transaction(this.m_doc, "剖面刷新");
				try
				{
					transaction.Start();
					this.m_doc.Delete(list);
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
					return false;
				}
				return true;
			}
			return true;
		}

		private ExternalCommandData m_commandData;

		public static Application g_application;

		public static WindowHandle _hWndRevit;

		private ElementId m_ViewId;

		private Document m_doc;

		private UIApplication m_uiApp;

		private int m_iflg;
	}
}
